<template>
  <div class="">
    <el-form :inline="true">
      <div style="display: flex; justify-content: space-between;">
        <div />
        <el-form-item>
          <el-button type="primary" @click="onVisible(1)">新增</el-button>
        </el-form-item>
      </div>
    </el-form>
    <el-table v-loading="listLoading" :data="tableData" border style="width: 100%">
      <el-table-column prop="mchid" label="商户id" width="100" align="center" />
      <el-table-column prop="appid" label="AppID" align="center" />
      <el-table-column prop="key" label="key" align="center" />
      <el-table-column prop="notifyUrl" label="回调地址" align="center" />
      <el-table-column prop="shopname" label="商户名称" align="center" />
      <el-table-column prop="sslcertpath" label="证书/文件地址" align="center" />
      <el-table-column prop="wechatpayserial" label="微信证书序列号" align="center" />
      <el-table-column label="操作" width="160px" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="onVisible(2, scope.row)">编辑</el-button>
          <!-- <el-button size="mini" type="danger" @click="onDelete(scope.row)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination v-if="totalCount > 10" layout="prev, pager, next" :total="totalCount"
      @current-change="idx => { PageIndex = idx }" />
    <el-dialog :title="visibleType == '1' ? '新增配置' : '修改配置'" :visible.sync="dialogVisible" width="500px"
      :close-on-click-modal="false" @closed="handleClose">
      <div>
        <el-form ref="forms" :rules="rules" :model="form" label-width="110px">
          <div class="flex" style="display: flex;">
            <el-form-item label="appid" prop="appid">
              <el-input v-model="form.appid"  />
            </el-form-item>
            <el-form-item label="appsecret" prop="appsecret">
              <el-input v-model="form.appsecret" />
            </el-form-item>
          </div>
          <el-form-item label="商户ID" prop="mchid">
            <el-input v-model="form.mchid" type="text" :rows="2" placeholder="请输入商户ID" />
          </el-form-item>
          <el-form-item label="key" prop="key">
            <el-input v-model="form.key" type="text" :rows="2" placeholder="请输入key" />
          </el-form-item>
          <el-form-item label="回调地址" prop="notifyUrl">
            <el-input v-model="form.notifyUrl" type="text" :rows="2" placeholder="请输入回调地址" />
          </el-form-item>
          <el-form-item label="商户名称" prop="shopname">
            <el-input v-model="form.shopname" type="text" :rows="2" placeholder="请输入商户名称" />
          </el-form-item>
          <el-form-item label="证书文件地址" prop="sslcertpath">
            <el-input v-model="form.sslcertpath" type="text" :rows="2" placeholder="请输入证书文件地址" />
          </el-form-item>
          <el-form-item label="微信证书序列号" prop="wechatpayserial">
            <el-input v-model="form.wechatpayserial" type="textarea" :rows="2" placeholder="请输入微信证书序列号" />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { WechatGetPage_SysConfig, WechatAddEdit, Delete_SysConfig } from '@/api/sysconfig.js'
export default {
  name: 'Name',
  data() {
    return {
      form: {
        appid: '',
        appsecret: '',
        mchid: '',
        key: '',
        notifyUrl: '',
        shopname: '',
        sslcertpath: '',
        wechatpayserial: '',
      },
      listLoading: false,
      tableData: [],
      totalCount: 0,
      PageIndex: 1,
      dialogVisible: false,
      visibleType: '',
      rules: {
        idx: [
          { required: true, message: '请输入ID', trigger: 'blur' }
        ],
        // val: [
        //   { required: true, message: '请输入配置内容', trigger: 'blur' },
        // ],
        comment: [
          { required: true, message: '请输入配置说明', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请输入配置类型', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  watch: {
    PageIndex(val) {
      console.log(val)
      this.getData()
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    async onDelete(item) {
      try {
        const confirm = await this.$confirm('此操作将删除该配置, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        if (confirm == 'confirm') {
          const { success, msg } = await Delete_SysConfig([item.idx])
          this.getData()
          this.$message({
            message: msg,
            type: success ? 'success' : 'error'
          })
        }
      } catch (e) {
        // 取消
        console.log(e)
      }
    },
    onEdit() {
      this.$refs['forms'].validate(async (valid) => {
        if (valid) {
          const { success, msg } = await WechatAddEdit(this.form)
          if (success) {
            this.dialogVisible = false
            this.getData()
          }
          this.$message({
            message: msg,
            type: success ? 'success' : 'error'
          })
        } else {
          return false
        }
      })
    },
    // 关闭前
    handleClose() {
      this.$refs.forms.resetFields()
    },
    // 打开前
    onVisible(type, item = {}) {
      this.visibleType = type
      this.form = { ...item }
      this.dialogVisible = true
    },
    async onSearch() { },
    async getData(address) {
      this.listLoading = true
      const { list, pageIndex, totalCount } = await WechatGetPage_SysConfig({
        PageIndex: this.PageIndex,
        PageSize: 10
      })
      this.totalCount = totalCount
      this.tableData = list
      this.listLoading = false
    }
  }
}
</script>

<style lang="scss" scoped></style>
<style lang="scss">
.el-table .warning-row {
  background: oldlace;
}
</style>
